package org.agridata.akvisualization.service.impl;

import org.agridata.akvisualization.mapper.MapMapper;
import org.agridata.akvisualization.result.Result;
import org.agridata.akvisualization.service.MapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.CompletableFuture;

@Service
public class MapServiceImpl implements MapService {

    @Autowired
    private MapMapper mapMapper;

    @Override
    public Result breedingStockAK() {
        try {
            List<Map<String, Object>> maps = mapMapper.selectBreedingStock();

            return Result.ok(maps);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("出现异常");
        }
    }

    @Override
    public Result countyByRegionId(Integer regionId,String pointType,String mid) {
        Map<String, Object> objectMap = new HashMap<>();
        List<Map<String, Object>> numList = mapMapper.selectNumByRegionId(regionId);
        String[] split = pointType.split(",");
        List<String> list = Arrays.asList(split);
        List<Map<String, Object>> messageList = mapMapper.selectMessageByRegionId(regionId, list,mid);
        objectMap.put("liebiao", numList);
        Map<String, Object> toGaoDeResult = this.toGaoDe(messageList);
        objectMap.put("ditu", toGaoDeResult);
        return Result.ok(objectMap);
    }

    private Map<String, Object> toGaoDe(List<Map<String, Object>> messageList) {

        Map<String, Object> sumMap = new HashMap<>();

//        Map<String, Object> districtMap = new HashMap<>();
        List<Map<String, Object>> sumList = new ArrayList<>();
        List<Map<String, Object>> dataList = new ArrayList<>();

//        Map<String, Object> dataMap = mapMapper.selectRegionLevelList(regionId);
//        districtMap.put("city", dataMap.get("RegionName"));
//        districtMap.put("center", dataMap.get("Longitude") + "," + dataMap.get("Latitude"));
//        dataList.add(districtMap);
        for (Map<String, Object> ss : messageList) {
            Map<String, Object> dituMap = new HashMap<>();
            Map<String, Object> dituDistrictMap = new HashMap<>();
            dituMap.put("lnglat", ss.get("Longitude") + "," + ss.get("Latitude"));
            dituMap.put("PlaceId", ss.get("PlaceId"));
            dituMap.put("city", ss.get("city"));
            dituMap.put("district", ss.get("district"));
            dituMap.put("area", ss.get("area"));
            dituMap.put("community", ss.get("community"));
            dituMap.put("building", ss.get("DisplayName"));
            dituMap.put("PointType", ss.get("PointType"));
            dituMap.put("FarmIntro", ss.get("FarmIntro"));
            dataList.add(dituDistrictMap);
            sumList.add(dituMap);
        }
        LinkedHashSet<Map<String, Object>> dataLists = new LinkedHashSet<>(dataList);
        sumMap.put("PlaceList", sumList);
        sumMap.put("district", dataLists);
        return sumMap;
    }
}
